<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="pagedData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="建议ID" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="支持数" align="center" prop="supportCount" />
      <el-table-column label="反对数" align="center" prop="opposeCount" />
      <el-table-column label="内容详情" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="showContent(scope.row.content)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="handlePageChange"
    />

    <!-- 内容详情对话框 -->
    <el-dialog title="内容详情" :visible.sync="contentDialogVisible" width="500px" append-to-body>
      <div v-html="currentContent"></div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="contentDialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { suggestVote } from "@/api/suggestion/suggestion"; // 你的接口路径

export default {
  name: "SuggestVoteList",
  data() {
    return {
      showSearch: true,  // ✅ 防止缺失报错
      loading: false,
      allData: [],        // 保存接口返回的所有数据
      pagedData: [],      // 当前分页数据
      total: 0,           // 总数据条数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
      },
      currentContent: '',
      contentDialogVisible: false,
      ids: [],
      single: true,
      multiple: true,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      this.loading = true;
      try {
        const response = await suggestVote(this.queryParams); // 直接返回数组
        let data = response;

        // 如果有搜索条件，前端简单过滤
        if (this.queryParams.title) {
          const keyword = this.queryParams.title.toLowerCase();
          data = data.filter(item => item.title.toLowerCase().includes(keyword));
        }

        this.allData = data;
        this.total = data.length;
        this.handlePageChange(); // 初始化分页
      } catch (err) {
        console.error('获取数据失败', err);
        this.$message.error('获取数据失败');
      } finally {
        this.loading = false;
      }
    },
    handlePageChange() {
      const start = (this.queryParams.pageNum - 1) * this.queryParams.pageSize;
      const end = start + this.queryParams.pageSize;
      this.pagedData = this.allData.slice(start, end);
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams.title = null;
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    showContent(content) {
      this.currentContent = content;
      this.contentDialogVisible = true;
    },
  }
};
</script>

<style scoped>
.el-button [class*=el-icon-] + span {
  margin-left: 3px;
}
</style>
